// pages/audioplayanimation/audioplayanimation.js
var countDownTimer;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    audioShowImg: '../../images/audio_play_03.png',
    audioPlayImgArr: [
      '../../images/audio_play_01.png',
      '../../images/audio_play_02.png',
      '../../images/audio_play_03.png',
    ],
    audioPlaying: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    if (countDownTimer) {
      clearInterval(countDownTimer)
      countDownTimer = null
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  tapAudioPlayBtn: function () {
    if (this.data.audioPlaying) { //正在播放时
      //暂停播放
      clearInterval(countDownTimer)
      countDownTimer = null
      this.setData({
        audioPlaying: false,
        audioShowImg: this.data.audioPlayImgArr[2]
      })
    }
    else {
      //开始或继续播放
      this.setData({
        audioPlaying: true,
      })

      var audioPlayImgArrIndex = 0
      this.setData({
        audioShowImg: this.data.audioPlayImgArr[audioPlayImgArrIndex]
      })
      countDownTimer = setInterval(() => {
        switch (audioPlayImgArrIndex) {
          case 0:
          case 1:
            audioPlayImgArrIndex++;
            break;
          case 2:
            audioPlayImgArrIndex = 0;
            break;
        }
        
        this.setData({
          audioShowImg: this.data.audioPlayImgArr[audioPlayImgArrIndex]
        })
      }, 700) //时间间隔为0.7秒
    }
  },
})